package com.jwdf.uda.http.action;

import android.content.Context;

import com.jwdf.uda.bean.WXPayment;
import com.jwdf.uda.http.ApiServiceModule;
import com.jwdf.uda.http.ApiWXPayService;
import com.jwdf.uda.utils.LogUtils;
import com.jwdf.uda.utils.NETUtils;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2016/8/19.
 */
public class WXPayAction {
    private final static String TAG = "WXPayAction";
    protected Context context;
    protected ApiWXPayService apiWXPayService;

    public WXPayAction(Context context) {
        this.context = context;
        apiWXPayService = ApiServiceModule.getInstance().providerApiWXPayService(context);
    }

    public interface IResCallback<T> {
        int RESULT_OK = 0;

        int RESULT_NOT_NET_ERROR = 1;

        int RESULT_NET_ERROR = 2;

        int RESULT_SERVICE_ERROR = 3;

        int RESULT_OTHER_ERROR = 4;

        void onCallback(int resultCode, WXPayment response);
    }

    /**
     * 发送请求数据
     *
     * @param iResCallback
     * @return
     */
    public Call request(final IResCallback<WXPayment> iResCallback) {
        if (!NETUtils.isNetworkConnected(context)) {
            //处理无网络情况
            if (iResCallback != null) {
                iResCallback.onCallback(IResCallback.RESULT_NOT_NET_ERROR, null);
            }
            return null;
        }
        try {
            Call<WXPayment> call = apiWXPayService.buildOrder();
            enqueue(call, iResCallback);
            return call;
        } catch (Throwable e) {
            e.printStackTrace();
            if (iResCallback != null) {
                iResCallback.onCallback(IResCallback.RESULT_OTHER_ERROR, null);
            }
        }
        return null;
    }

    private void enqueue(Call<WXPayment> call, final IResCallback<WXPayment> iResCallback) {
        call.enqueue(new Callback<WXPayment>() {
            @Override
            public void onResponse(Call<WXPayment> call, Response<WXPayment> response) {
                LogUtils.i(TAG, "onResponse");
                int responseCode = response.code();
                if (responseCode == 200) {
                    if (iResCallback != null) {
                        iResCallback.onCallback(IResCallback.RESULT_OK, response.body());
                    }
                } else {
                    try {
                        String msg = new String(response.errorBody().bytes());
                        LogUtils.i(TAG, "onResponse responseCode:" + responseCode + "," + msg);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    if (iResCallback != null) {
                        iResCallback.onCallback(IResCallback.RESULT_SERVICE_ERROR, null);
                    }
                }
            }

            @Override
            public void onFailure(Call<WXPayment> call, Throwable t) {
                LogUtils.i(TAG, "onFailure:");
                t.printStackTrace();
                if (iResCallback != null) {
                    iResCallback.onCallback(IResCallback.RESULT_NET_ERROR, null);
                }
            }
        });
    }

}
